package com.lxl.view;

import java.awt.EventQueue;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.lxl.model.Book;
import com.lxl.service.BookService;
import com.lxl.service.impl.BookServiceImpl;

import javax.swing.JLabel;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;





import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.awt.Cursor;
import java.awt.ScrollPane;

/**
 * @author 刘星鹭
 * @Email liuxinglumail@163.com
 * @version 创建时间： 2014年9月12日 上午11:43:05
 */

public class ViewBook extends JFrame {

	static BookService bookService = new BookServiceImpl();
	static String[][] bookList = new String[100][2];
	static List<Book> list = bookService.list();
	
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;
	private JLabel lblNewLabel;
	private JButton button_1;
	private JButton button_2;
	
	public static void main(String[] args) {

		for (int j = 0, i = 0; j < list.size(); j++) {
			if (i == 0) {
				bookList[j][i] = list.get(j).getBookNo().toString();
				i = 1;
			}
			if (i == 1) {
				bookList[j][i] = list.get(j).getBookName();
				i = 0;
			}
		}

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ViewBook frame = new ViewBook();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public ViewBook() {
		setFont(new Font("微软雅黑", Font.PLAIN, 12));
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				ManagerView.main(new String[1]);
			}
		});
		setTitle("图书管理系统");		
		setBounds(100, 100, 450, 277);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lblNewLabel = new JLabel("书名");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		lblNewLabel.setBounds(309, 5, 65, 15);
		contentPane.add(lblNewLabel);

		JLabel label = new JLabel("书号");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label.setBounds(82, 5, 55, 15);
		label.setAlignmentX(Component.CENTER_ALIGNMENT);
		contentPane.add(label);
		
		JButton button = new JButton("删除书籍");
		button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				DeleteBook.main(new String[1]);
				dispose();
			}
		});
		
		ScrollPane scrollPane = new ScrollPane();
		scrollPane.setBounds(0, 24, 434, 175);
		contentPane.add(scrollPane);
		

		table = new JTable();
		table.setEnabled(false);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		table.setBounds(5, 20, 424, 175);
		table.setModel(new DefaultTableModel(bookList,
			new String[] {
				"\u7528\u6237\u7F16\u53F7", "\u7528\u6237\u59D3\u540D"
			}
		));
		scrollPane.add(table);
		button.setBounds(118, 205, 93, 23);
		contentPane.add(button);
		
		button_1 = new JButton("修改书籍");
		button_1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				MergeBook.main(new String[1]);
				dispose();
			}
		});
		button_1.setBounds(221, 205, 93, 23);
		contentPane.add(button_1);
		
		button_2 = new JButton("添加书籍");
		button_2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				AddBook.main(new String[1]);
				dispose();
			}
		});
		button_2.setBounds(15, 205, 93, 23);
		contentPane.add(button_2);
		
		JButton btnNewButton = new JButton("刷新");
		btnNewButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
				list = bookService.list();
				for (int j = 0, i = 0; j < list.size(); j++) {
					if (i == 0) {
						bookList[j][i] = list.get(j).getBookNo().toString();
						i = 1;
					}
					if (i == 1) {
						bookList[j][i] = list.get(j).getBookName();
						i = 0;
					}
				}
				table.setModel(new DefaultTableModel(bookList,
						new String[] {
							"\u7528\u6237\u7F16\u53F7", "\u7528\u6237\u59D3\u540D"
						}
					));
				JOptionPane.showMessageDialog(null, "刷新成功！");
			}
		});
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		btnNewButton.setBounds(324, 205, 93, 23);
		contentPane.add(btnNewButton);
	}
}
